/**
 * 
 */
package com.netscenery.yang.model;



import java.util.ArrayList;

import com.netscenery.util.ErrTag;



/**
 * @author llly
 *
 */
public class Choice extends Node {
	private boolean isUseDefaultMandatory = true;
	private boolean mandatory;
	private String defaultValue;
	
	
	public Choice(Env env,String name) throws ModelException{
		super(env,name);
	}
	
	

	public boolean isUseDefaultMandatory() {
		return isUseDefaultMandatory;
	}



	/**
	 * @return the mandatory
	 */
	public boolean isMandatory() {
		return mandatory;
	}

	/**
	 * @param mandatory the mandatory to set
	 */
	public void setMandatory(boolean mandatory) {
		if (this.mandatory == mandatory){
			return;
		}
		this.mandatory = mandatory;
		this.isUseDefaultMandatory = false;
	}

	/**
	 * @return the defaultValue
	 */
	public String getDefaultValue() {
		return defaultValue;
	}

	/**
	 * @param defaultValue the defaultValue to set
	 */
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}
	public ArrayList<Node> getDataSons(){
		int size = getSonsNumber();
		if (0 == size){
			return null;
		}
		
		ArrayList<Node> nodes = new ArrayList<Node>(1);
		
		for (int i = 0; i < size; i++){
			Case c= (Case) getSon(i);
			if(null == c){
				continue;
			}
			
			nodes.addAll(c.getDataSons());
			
		}
		
		return nodes;
	}

	/* (non-Javadoc)
	 * @see com.netscenery.yang.model.Node#validate()
	 */
	@Override
	public Validator validate() {
		
		Validator validator = new Validator();
		int size = getSonsNumber();
		//at least one case
		if (size < 1){
			validator.setIsValid(false);
			validator.addMsg(new Message(this,ErrTag.ERROR,"choice must have at least one case."));
		}
		
		// all sons must be case type
		for (int i = 0; i < size; i++){
			DataDefElement son = getSon(i);
			if (null == son){
				continue;
			}
			
			if (!(son instanceof Case)){
				validator.setIsValid(false);
				validator.addMsg(new Message(this,ErrTag.ERROR,"choice'sons must be case type."));

			}
			Validator caseValidator = son.validate();
			validator.copy(caseValidator);
		
		}
		
		
		//if has defaultvalue, it must be one case's name
		if (null != defaultValue){
			int i = 0;
			DataDefElement son = null;
			for (i = 0; i < size; i++){
				son = getSon(i);
				if (null == son){
					continue;
				}
				
				if (defaultValue.equals(son.getName())){
					break;
				}
				
			}
			
			if (i == size){
				validator.setIsValid(false);
				validator.addMsg(new Message(this,ErrTag.ERROR,ErrorCode.INVALID_DEFAULTVALUE));

			}
			
		}
		//if is mandatory , defaultvalue must be null
		if (mandatory){
			if (null != defaultValue){
				validator.setIsValid(false);
				validator.addMsg(new Message(this,ErrTag.ERROR,ErrorCode.MANDATORY_HASDEFAULT));

			}
		}
		
		
		
		
		Validator sValidator = super.validate();
		validator.copy(sValidator);
		return validator;
	}



	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}

}
